package com.zentrale1.noodlemail;

import java.util.Date;

/*
 * The MIT License
 * 
 * Copyright (c) 2010 Andreas Fleig
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

/**
 * Data object for mails. "Comparable" ordering is based on the date header.
 */
public class Mail implements Comparable<Mail>
{
	private String subject;
	private String messageId;
	private String textBody;
	private String replyTo;
	private String folder;
	private String path;
	private String fromAddress;
	private String fromName;
	private Date date;
	
	public Mail()
	{
		subject = "";
		messageId = "";
		textBody = "";
		replyTo = "";
	}
	
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getMessageId() {
		return messageId;
	}
	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}
	public String getTextBody() {
		return textBody;
	}
	public void setTextBody(String textBody) {
		this.textBody = textBody;
	}
	public void setReplyTo(String replyTo) {
		this.replyTo = replyTo;
	}
	public String getReplyTo() {
		return replyTo;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public String getFolder() {
		return folder;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getPath() {
		return path;
	}
	
	public void setFrom(String from)
	{
		if (from.matches("[^<]+<[^>]+>")) {
			String[] fromTokens = from.split(" ");
			int i;
			fromName = "";
			for (i=0; i<fromTokens.length-2; i++) {
				fromName = fromName + fromTokens[i] + " ";
			}
			fromName = fromName + fromTokens[i];
			fromAddress = fromTokens[i+1].substring(1);
			fromAddress = fromAddress.substring(0, fromAddress.length()-1);
		} else {
			fromAddress = from;
		}
	}
	
	public String getFromAddress()
	{
		return fromAddress;
	}
	
	public String getFromName()
	{
		return fromName;
	}

	public void setDate(Date date) 
	{
		this.date = date;
	}

	public Date getDate() 
	{
		return date;
	}

	@Override
	public int compareTo(Mail o) 
	{
		//return this.date.compareTo(o.date);
		return o.date.compareTo(this.date);
	}
	
}
